package com.stox.client.ui.javafx.widget;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.control.TitledPane;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;

import com.stox.client.core.model.intf.Searchable;
import com.stox.client.ui.javafx.core.util.CSS;
import com.stox.client.ui.javafx.core.util.UI;
import com.stox.shared.i18n.MessageSource;

public class SearchPanel<T> extends TitledPane {

	private final TextField textField;
	private final Searchable searchable;
	
	public SearchPanel(Searchable searchable) {
		this.searchable = searchable;
		this.textField = createTextField();
		setText(MessageSource.get("Search"));
		setExpanded(false);
		setAnimated(false);
		StackPane contentPane = new StackPane();
		UI.style(contentPane, CSS.searchPanel);
		setContent(contentPane);
		contentPane.getChildren().add(textField);
		addDisplayHandlers();
	}
	
	private TextField createTextField(){
		CustomTextField textField = new CustomTextField();
		textField.setPromptText(MessageSource.get("Enter.search.term"));
		textField.setRight(new HBox(createNextButton(), createPreviousButton()));
		textField.textProperty().addListener(new ChangeListener<String>(){
			@Override
			public void changed(ObservableValue<? extends String> observable,
					String oldValue, String newValue) {
				searchable.search(newValue, true, false);
			}
		});
		return textField;
	}
	
	private Button createPreviousButton(){
		Button previousButton = new Button();
		UI.style(previousButton, CSS.upArrow);
		previousButton.setOnAction(new EventHandler<ActionEvent>(){
			@Override
			public void handle(ActionEvent event) {
				searchable.search(textField.getText(), false, true);
			}
		});
		return previousButton;
	}
	
	private Button createNextButton(){
		Button nextButton = new Button();
		UI.style(nextButton, CSS.downArrow);
		nextButton.setOnAction(new EventHandler<ActionEvent>(){
			@Override
			public void handle(ActionEvent event) {
				searchable.search(textField.getText(), true, true);
			}
		});
		return nextButton;
	}
	
	private void addDisplayHandlers(){
		Node node = (Node)searchable.getNavigable().getTarget();
		EventHandler<KeyEvent> eventHandler = new EventHandler<KeyEvent>(){
			@Override
			public void handle(KeyEvent event) {
				if(event.getCode().isLetterKey()){
					setExpanded(true);
					getTextField().requestFocus();
				}else if(KeyCode.ESCAPE.equals(event.getCode()) || KeyCode.ENTER.equals(event.getCode())){  // TODO Should come from settings
					setExpanded(false);
				}
			}
		};
		addEventHandler(KeyEvent.KEY_PRESSED, eventHandler);
		node.addEventHandler(KeyEvent.KEY_PRESSED, eventHandler);
		node.addEventHandler(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>(){
			@Override
			public void handle(MouseEvent event) {
				setExpanded(false);
			}
		});
	}
	
	public TextField getTextField(){
		return textField;
	}
	
}
